//
//  LzwBnimationWebView.m
//  CoJellyCartoon
//
//  Created by 刘志武 on 16/4/7.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "LzwBnimationWebView.h"
#import <WebKit/WebKit.h>

@interface LzwBnimationWebView ()<WKNavigationDelegate, WKUIDelegate>

@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) UIProgressView *sliderView;

@end

@implementation LzwBnimationWebView

- (void)dealloc
{
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress" context:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 20, 20);
    [button setImage:[[UIImage imageNamed:@"L20.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(LeftButtonAxtion:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = barItem;
    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"L20.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(LeftButtonAxtion:)];
    
    
    self.webView = [[WKWebView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:self.self.webView];
    
    [GiFHUD show];
    
    [GiFHUD setGifWithImageName:@"hold.gif"];
    
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webViewUrl]]];
    
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    
    self.sliderView = [[UIProgressView alloc]init];
    self.sliderView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.sliderView];
    
    [self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.view).offset(0);
        make.left.equalTo(self.view);
        make.width.equalTo(self.view);
        make.height.mas_equalTo(3);
        
        
    }];
    
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:0 context:nil];
    

}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
    self.sliderView.progress = self.webView.estimatedProgress;
    
    //(@",,,,,");
}


- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    
    
    [GiFHUD dismiss];
    
}


- (void)LeftButtonAxtion:(UIButton *)bart{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
